#!/bin/bash

if [ $2 ]; then
        SUBNAME="_$2";
        SUBTEXT=" $2";
        SUB=$2
fi

BINDIR=/usr/local/ntripcaster/bin
VARDIR=/usr/local/ntripcaster/var
PIDFILE=$VARDIR/caster$SUBNAME.pid
WATCHPIDFILE=$VARDIR/casterwatch$SUBNAME.pid

stop() {
        RUN_PID=`ps aux | grep \`cat $WATCHPIDFILE\` | grep -v grep | awk '{ print $2}'`
        PID=`cat $WATCHPIDFILE`

        if ( [ $RUN_PID ] && [ $PID ] && [ $RUN_PID=$PID ] ); then
                echo "Stopping Ntrip caster$SUBTEXT..."
                kill `cat $WATCHPIDFILE`
        else
                echo "Could not find$SUBTEXT casterwatch PID file"
        fi
}

start() {
        if (`test -r $PIDFILE`) then
                RUN_PID=`ps aux | grep \`cat $PIDFILE\` | grep -v grep | awk '{ print $2}'`
                PID=`cat $PIDFILE`
        fi

        if ( [ $RUN_PID ] && [ $PID ] && [ $RUN_PID=$PID ] ); then
                echo "Already running$SUBTEXT ($PID)"
                exit 1
        fi

        echo "Starting Ntrip caster$SUBTEXT..."
        $BINDIR/casterwatch $SUB &
        echo $! > $WATCHPIDFILE
}

if [ ! $1 ]; then
        echo "No command given (start|stop|restart) <subcaster>"
        exit -1
elif [ $1 = "start" ]; then
        start
elif [ $1 = "stop" ]; then
        stop
elif [ $1 = "restart" ]; then
        stop
        sleep 5
        echo "Restarting$SUBTEXT..."
        start
fi

exit 1
